<?php

	class Upload extends arfaController
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model("master_upload_model");
		}
		
		public function index($id = NULL)
		{
			$data["id"]		= $id;
			$data["foto"]	= $this->master_upload_model->get_foto($id);
			$this->load->view("v_upload",$data);
		}
		
		public function save_image($id = NULL)
		{
			$directory 	= 'uploads/foto_ruas/'.$id;
			$thumb		= 'uploads/foto_ruas/'.$id.'/thumb';
		
			if (!is_dir($directory))
			{
				mkdir($directory, 0777, true);
				$dir_exist 	= false;
			}
			
			if (!is_dir($thumb))
			{
				mkdir($thumb, 0777, true);
				$dir_exist 	= false;
			}
		
			$config['upload_path'] 		= $directory;
			$config['allowed_types'] 	= 'jpg|png';
			$config['max_size']			= '100000';
			$config['max_width']  		= '10000';
			$config['max_height']  		= '10000';    
			$this->load->library('upload');
			$this->upload->initialize($config);

			if ( !$this->upload->do_upload('photo'))
			{
				//echo $this->upload->display_errors();
				echo "Gagal Mengupload";
				echo "<br><a href='".site_url('upload/index/'.$id)."'>Kembali</a>";
			} 
			else
			{
				$foto 		= $this->upload->data();
				$file_name 	= $foto['raw_name'].$foto['file_ext'];
				
				$this->load->library('resize_image');
				$this->resize_image->do_resize($directory,$thumb,$file_name);
				
				$data = array(
					"ID_RUAS"		=> $id,
					"CAPTION"		=> $this->input->post("caption_foto"),
					"PATH_FOTO"		=> $file_name,
					"THUMB"			=> $foto['raw_name'].'_thumb'.$foto['file_ext']
				);
				
				if($this->master_upload_model->save($data))
				{
					redirect('upload/index/'.$id);
				}
				else
				{
					echo "Ada kesalahan !";
				}
			}
		}
		
		public function delete_image($id = NULL, $id2 = NULL)
		{
			$this->master_upload_model->delete($id);
			redirect('upload/index/'.$id2);
		}
	}